<?php
/*
 * Create Image's thumbnail on-the-fly
 *
 * PHP version 5
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author 	  Nashruddin Amin <me@nashruddin.com>
 * @copyright Nashruddin Amin 2008
 * @license	  GNU General Public License 3.0
 * @version   1.0
 */

class Image {
	private	$_img_path;
	private	$_img_default = "na.png";
	private	$_fname;
	private	$_scale;
	private	$_handle;
	private $_img_type;
	private	$_width;
	private	$_height;
	
	/**
	 * class constructor
	 */
	public function __construct()
	{
	}
	
	/**
	 * load image
	 *
	 * @param string $fname	image name
	 * @param float  $scale scale for displayed image
	 */
	public function loadImage($fname, $scale = '')
	{		
		$this->_scale = (empty($scale)) ? 1.0 : $scale;
		
		if (!empty($fname) && file_exists("$this->_img_path/$fname")) {
			$this->_fname = "$this->_img_path/$fname";			
		} else {
			$this->_fname = "$this->_img_path/$this->_img_default";
		}
		list($this->_width, $this->_height, $this->_img_type) = getimagesize($this->_fname);
		
		//maksa begin (by AWG  *dipaksa dgn skala tinggi menjadi $_GET['h'])
		$this->_scale = $_GET['h']/$this->_height;
		//maksa end
		
		switch ($this->_img_type) {
			case IMAGETYPE_PNG:
				$img_orig = imagecreatefrompng($this->_fname);
				break;			
			case IMAGETYPE_JPEG:
				$img_orig = imagecreatefromjpeg($this->_fname);
				break;			
			case IMAGETYPE_GIF:
				$img_orig = imagecreatefromgif($this->_fname);
				break;
			default:
				break;
		}		
		$new_w = $this->_width * $this->_scale; 
		$new_h = $this->_height * $this->_scale;
		$this->_handle = imagecreatetruecolor($new_w, $new_h);
		
		imagecopyresampled($this->_handle, $img_orig, 0, 0, 0, 0,
							$new_w, $new_h, $this->_width, $this->_height);
	}
	
	/**
	 * display image to browser
	 */
	public function outToBrowser()
	{
		switch ($this->_img_type) {
			case IMAGETYPE_PNG:
				header("Content-type: image/png");
				imagepng($this->_handle);
				break;
				
			case IMAGETYPE_JPEG:
				header("Content-type: image/jpeg");
				imagejpeg($this->_handle, null, 100);
				break;
				
			case IMAGETYPE_GIF:
				header("Content-type: image/gif");
				imagegif($this->_handle);
				break;
			
			default:
		}		
		imagedestroy($this->_handle);
	}
	
	/**
	 * set the directory containing images
	 */
	public function setImgPath($path)
	{
		$this->_img_path = $path;
	}
	
	/**
	 * set default image
	 */
	public function setDefaultImage($img)
	{
		$this->_img_default = $img;
	}
}

/* change this to your images directory */
$imgdir = "./repositories/photos/";

$image = new Image;
$image->setImgPath($imgdir);
$image->loadImage($_GET['filename'], $_GET['scale']);
$image->outToBrowser();
